$(function(){
	$(".js-open-cart").click(function(event){
		event.stopPropagation();
		event.preventDefault();
		$('.title').text(this.getAttribute('cart-title'));
		$('.price-integer').text(this.getAttribute('cart-price'));
		$('.cart-stock-num').text(this.getAttribute('cart-num'));
        $("#main-img").attr('src',this.getAttribute('cart-img'));
		$("#goods_id").val(this.getAttribute('cart-gid'));
        $("#cart").val(this.getAttribute('cart-type'));
		$('.modal-in').show();
		$('.popup-overlay').show();
	})
	$(".close-popup").click(function(){
		$('.modal-in').hide();
		$('.popup-overlay').hide();
	})
    $('.cart-num-minus1').click(function(){
        var n = $(this).parent().parent().parent().find('.cart-num1').val();
        var goods_id =$(this).parent().parent().parent().find('.goods_id').val();
        var num = parseInt(n)-1;
        if (num == 0) {
            return
        }

        $(this).parent().parent().parent().find('.cart-num1').val(num)
    })
    $('.cart-num-plus1').click(function(){
        var n =$(this).parent().parent().parent().find('.cart-num1').val();
        var stockNum = $(this).parent().parent().parent().parent().find('.cart-stock-num').text();
        //添加购物车
        var goods_id =$(this).parent().parent().parent().find('.goods_id').val();

        var num = parseInt(n)+1;
        if(num > stockNum){
            return
        }

        $(this).parent().parent().parent().find('.cart-num1').val(num)
    })
    $('.cart-num1').blur(function(){
        var n = $(this).parent().parent().parent().find('.cart-num1').val();

        var stockNum = $(this).parent().parent().parent().parent().find('.cart-stock-num').text();
        var goods_id =$(this).parent().parent().parent().find('.goods_id').val();
        if (parseInt(n) > parseInt(stockNum)) {

            $(this).parent().parent().parent().find(".cart-num1").val(stockNum);
        }
        if(parseInt(n) <= 0){

            $(this).parent().parent().parent().find(".cart-num1").val(1);
        }


    })
    $('.cart-num-minus').click(function(){
        var n = $(this).parent().parent().parent().find('.cart-num').val();
        var goods_id =$(this).parent().parent().parent().find('.goods_id').val();
        var num = parseInt(n)-1;
        if (num == 0) {
            return
        }
        updateCart(goods_id,num);
        $(this).parent().parent().parent().find('.cart-num').val(num)
    })

    $('.cart-num-plus').click(function(){
        var n =$(this).parent().parent().parent().find('.cart-num').val();
        var stockNum = $(this).parent().parent().parent().parent().find('.cart-stock-num').text();
        //添加购物车
       	var goods_id =$(this).parent().parent().parent().find('.goods_id').val();

        var num = parseInt(n)+1;
        if(num > stockNum){
            return
        }
        updateCart(goods_id,num);
        $(this).parent().parent().parent().find('.cart-num').val(num)
    })

    $('.cart-num').blur(function(){
        var n = $(this).parent().parent().parent().find('.cart-num').val();

        var stockNum = $(this).parent().parent().parent().parent().find('.cart-stock-num').text();
        var goods_id =$(this).parent().parent().parent().find('.goods_id').val();
        if (parseInt(n) > parseInt(stockNum)) {
            updateCart(goods_id,stockNum);
            $(this).parent().parent().parent().find(".cart-num").val(stockNum);
        }
        if(parseInt(n) <= 0){
            updateCart(goods_id,1);
            $(this).parent().parent().parent().find(".cart-num").val(1);
        }
        updateCart(goods_id,n);

    })
    $('.cart-head-btn').click(function(){
        if ($(this).text() == '编辑' ) {
            $(this).text('完成')
            $('.cart-bottom-pay').hide()
            $('.cart-pay-box').show()
        }else{
            $(this).text('编辑')
            $('.cart-bottom-pay').show()
            $('.cart-pay-box').hide()
        }
    })
    $('.cart-selectAll').click(function(){
        if($(this).attr('checked')){
            $('.cart-el').each(function(){
                $(this).attr('checked',true);
            });
        }else{
            $('.cart-el').each(function(){
                $(this).attr('checked',false);
            })
        }
    })

    // 初始化金额，数量
    var initNum=0;
    var initPrice=0;
    $('.cart-el').each(function(){
        if($(this).attr('checked')){
            var num = $(this).parent().parent().find('.cart-num').val()
            initNum = parseInt(num)+initNum
        }
    })
    $('.cart-el').each(function(){
        if($(this).attr('checked')){
            var num = $(this).parent().parent().find('.cart-num').val()
            var price = $(this).parent().parent().find('.goods-price-num').text()
            initPrice = parseInt(num)*parseFloat(price)+initPrice;
        }
    })
    $('.cart-total-price').text(initPrice);
    $('.total-num').text(initNum);

    // 计算总金额
    function getAllPrice(){
        var sum = 0;
        $('.cart-el').each(function(){
            if($(this).attr('checked')){
                var num = $(this).parent().parent().find('.cart-num').val()
                var price = $(this).parent().parent().find('.goods-price-num').text()
                sum = parseInt(num)*parseFloat(price)+sum
            }
        })
        return sum;
    }
    function getAllGoodsId(){
        var goods_id=[];
        console.log(11);
        $('.cart-el').each(function(){
            if($(this).attr('checked')){
                var num=$(this).parent().parent().find('.goods_id').val();
                console.log(num);
                goods_id.push( parseInt(num))
            }
        })
        return goods_id;
    }
    // 计算总数量
    function getAllNum(){
        var sum = 0;
        $('.cart-el').each(function(){
            if($(this).attr('checked')){
                var num = $(this).parent().parent().find('.cart-num').val()

                sum = parseInt(num)+sum
            }
        })
        return sum;
    }
    function checkAllChecked(){
        $('.cart-el').each(function(){
            if(!$(this).attr('checked')){
                $('.cart-selectAll').attr('checked',false)
            }
        })
    }
    $('.cart-el').change(function(){
        checkAllChecked();
        var totalPrice = getAllPrice();
        var totalNum = getAllNum();
        $('.cart-total-price').text(totalPrice);
        $('.total-num').text(totalNum);
    })
    $('.cart-selectAll').change(function(){
        var totalPrice = getAllPrice();
        var totalNum = getAllNum();
        $('.cart-total-price').text(totalPrice);
        $('.total-num').text(totalNum);
    })
    $('.cart-num-minus').click(function(){
        var totalPrice = getAllPrice();
        var totalNum = getAllNum();
        $('.cart-total-price').text(totalPrice);
        $('.total-num').text(totalNum);
    })
    $('.cart-num-plus').click(function(){
        var totalPrice = getAllPrice();
        var totalNum = getAllNum();
        $('.cart-total-price').text(totalPrice);
        $('.total-num').text(totalNum);
    })

    $('.cart-num').blur(function(){
        var totalPrice = getAllPrice();
        var totalNum = getAllNum();
        $('.cart-total-price').text(totalPrice);
        $('.total-num').text(totalNum);
    })


    // 删除功能
    $('.cart-del-btn').click(function(){
        var goods_id=getAllGoodsId();
        $('.cart-el').each(function(){
            if($(this).attr('checked')){
                $(this).parent().parent().remove();
            }
        })
        var totalPrice = getAllPrice();
        var totalNum = getAllNum();
 ;
        // console.log(goods_id);
        delCart(goods_id);
        $('.cart-total-price').text(totalPrice);
        $('.total-num').text(totalNum);
    })
	//购买按钮
	$('.cart-buy-btn').click(function(){

		var type=$('#cart').val();
		var good_id=$('#goods_id').val();
		var num=$('.cart-num').val();
        // console.log(good_id);
		if(type=='cart'){//加入购物车操作

		}else{//直接购买

		    //todo
		}
	})
    $('.select-icon').click(function(){
        $('.overlay-select').hide();
    })
	$('.team-select').click(function(){
	    $('.overlay-select').show();
    })
    $('.if-custom').click(function(){
        $('.overlay-service').show()
    })
    $('.overlay-service').click(function(){
        $(this).hide();
    })
    $('.view-top img').click(function(){
        return false
    })
    $('.view-bottom img').click(function(){
        return false
    })
})
//加入购物车
function addCart(good_id,num){
	if(num<0){
        layer.open({
            content: '购买商品数量要大于0',
            time: 2
            ,skin: 'msg'

        });
		return ;
    }
var good_id=good_id;
	var num=num;

    $.post('/cart/add',{good_id:good_id,num:num},function(data){
    	if(data.code==0){
            layer.open({
				content: data.message,
                time: 2
                ,skin: 'msg'
            });
            return ;
		}else if(data.code==1){
            $('.modal-in').hide();
            $('.popup-overlay').hide();
            layer.open({
                content: '添加购物车成功',
                time: 2,
                skin: 'msg'
            });
		}
	});

    return ;
}
function delCart(goodsId){
    $.post('/cart/delete',{goods_ids:goodsId},function(data){
        if(data.code==0){
            layer.open({
                content: data.message,
                time: 2
                ,skin: 'msg'
            });
            window.reload;
        }else if(data.code==1){


        }
    })
}
function  settlementCart(){
    layer.open({
        type: 2
        ,content: '创建订单中'
    });

    var goods_id=[];

    $('.cart-el').each(function(){
        if($(this).attr('checked')){
            var num=$(this).parent().parent().find('.goods_id').val();
            // console.log(num);
            goods_id.push( parseInt(num))
        }
    })
    $.post('/cart/settlement',{goods_ids:goods_id},function(data){
        if(data.code==0){
            layer.open({
                content: data.message,
                time: 2
                ,skin: 'msg'
            });
            layer.closeAll();
            window.reload;
        }else if(data.code==1){

            layer.open({
                content: data.message,
                time: 2
                ,skin: 'msg'
            });
            layer.closeAll();
            location.href='/order/'+data.orderId;

        }
    })
}
//直接购买
function fastSettlement(goods_id,num){
    layer.open({
        type: 2
        ,content: '创建订单中'
    });
    $.post('/cart/fastSettlement',{goods_id:goods_id,num:num},function(data){
        layer.closeAll();
        if(data.code==0){
            layer.open({
                content: data.message,
                time: 2,
                skin: 'msg'
            });

            window.reload;
        }else if(data.code==1){

            layer.open({
                content: data.message,
                time: 2,
                skin: 'msg'
            });
            // layer.closeAll();
            location.href='/order/'+data.orderId;
        }
    })
}
//修改购物车
function updateCart(good_id,num){

    $.post('/cart/update',{good_id:good_id,num:num},function(data){
        if(data.code==0){
            layer.open({
                content: data.message,
                time: 2
                ,skin: 'msg'
            });
            window.reload;
        }else if(data.code==1){
            $('.modal-in').hide();
            $('.popup-overlay').hide();

        }
    });


}
function delivered(order_id) {
    var num = false;
    layer.open({
        content: '确定要确认收货么'
        ,btn: ['确认', '取消']
        ,yes: function(index) {
            if (num == true){
                return false
            }
            num = true
                $.post('/my/order/' + order_id + '/received', {}, function (data) {
                    num = false
                    if (data.code == 0) {
                        layer.open({
                            content: data.message,
                            time: 2
                            , skin: 'msg'
                        });
                        return;
                    } else if (data.code == 1) {

                        layer.open({
                            content: '确认收货成功',
                            time: 2
                            , skin: 'msg'
                        });
                        location.reload();
                        layer.close(index);
                    }
                });


        }
    });
}
function applyRefund(order_id){
    var num = false;
    layer.open({
        content: '确定要退款么'
        ,btn: ['确认', '取消']
        ,yes: function(index){
            if (num == true){
                return false
            }
            num = true
                $.post('/my/orders/' + order_id + '/apply_refund', {}, function (data) {
                    num = false
                    if (data.code == 0) {
                        layer.open({
                            content: data.message,
                            time: 2
                            , skin: 'msg'
                        });
                        return;
                    } else if (data.code == 1) {

                        layer.open({
                            content: '确认退款成功,退款将按照原来的支付方式退回',
                            time: 2
                            , skin: 'msg'
                        });
                        setTimeout(function(){
                            location.reload();
                            layer.close(index);
                        },2000);

                    }
                });
            }


    });
}
function orderCancel(order_id){
    var num = false;
    layer.open({
        content: '确定要取消订单么'
        ,btn: ['确认', '取消']
        ,yes: function(index){
            if (num == true){
                return false
            }
            num = true
            $.post('/my/orders/'+order_id+'/cancel',{},function(data){
                num = false
                if(data.code==0){
                    layer.open({
                        content: data.message,
                        time: 2
                        ,skin: 'msg'
                    });
                    return ;
                }else if(data.code==1){

                    layer.open({
                        content: '订单已取消',
                        time: 2
                        ,skin: 'msg'
                    });
                    location.reload();
                    layer.close(index);
                }
            });

        }
    });
}